package com.ys.controller.web.finance;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.entity.finance.TcwRunningBack;
import com.ys.entity.finance.TcwRunningBackDetail;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.finance.TcwRunningBackService.CheckForAddDto;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/finance/tcwRunningBack")
public class WebTcwRunningBackController {
	
	@Autowired
	private CommonApiService<TcwRunningBack> commonApiService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_RunningBack);
			
			commonApiService.toAdd(model, new TcwRunningBack().setBid(bid), MenuId.TCW_RunningBack, null, null);
			
			return "finance/tcwRunningBack/toAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 审核
	 */
	@RequiresPermissions("tcwRunningBack:flow")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		if (bid == null || bid.length() == 0) {
			return W.error(model, "单号不能为空");
		}
		
		TcwRunningBack entity = new TcwRunningBack().setBid(bid).queryFirst();
		
		try {
			commonApiService.toEdit(model, entity.getId(), new TcwRunningBack(), MenuId.TCW_RunningBack, null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		List<TcwRunningBackDetail> listTcwRunningBackDetail = new TcwRunningBackDetail()
				.setPbid(entity.getBid()).queryList();

		// 账单详情
		List<CheckForAddDto.RowDto> rows = new ArrayList<CheckForAddDto.RowDto>();
		
		for(TcwRunningBackDetail tcwRunningBackDetail : listTcwRunningBackDetail) {
			Long runningIdOne = tcwRunningBackDetail.getRunningIdOne();
			String vgsbmFromOne = tcwRunningBackDetail.getVgsbmFromOne();
			String vgsjcFromOne = tcwRunningBackDetail.getVgsjcFromOne();
			String vgsbmToOne = tcwRunningBackDetail.getVgsbmToOne();
			String vgsjcToOne = tcwRunningBackDetail.getVgsjcToOne();
			String vjylxOne = tcwRunningBackDetail.getVJylxOne();
			BigDecimal fjeOne = tcwRunningBackDetail.getFjeOne();
			String vbzOne = tcwRunningBackDetail.getVbzOne();

			Long runningIdTwo = tcwRunningBackDetail.getRunningIdTwo();
			String vgsbmFromTwo = tcwRunningBackDetail.getVgsbmFromTwo();
			String vgsjcFromTwo = tcwRunningBackDetail.getVgsjcFromTwo();
			String vgsbmToTwo = tcwRunningBackDetail.getVgsbmToTwo();
			String vgsjcToTwo = tcwRunningBackDetail.getVgsjcToTwo();
			String vjylxTwo = tcwRunningBackDetail.getVJylxOne();
			BigDecimal fjeTwo = tcwRunningBackDetail.getFjeTwo();
			String vbzTwo = tcwRunningBackDetail.getVbzTwo();
			rows.add(new CheckForAddDto.RowDto(runningIdOne, vgsbmFromOne, vgsjcFromOne, vgsbmToOne, vgsjcToOne, vjylxOne, fjeOne, vbzOne, runningIdTwo, vgsbmFromTwo, vgsjcFromTwo, vgsbmToTwo, vgsjcToTwo, vjylxTwo, fjeTwo, vbzTwo));
		}
		
		CheckForAddDto dto = new CheckForAddDto(rows);
		model.addAttribute("sonlist", dto);
		
		YsProcInst yspProcInst = new YsProcInst().setBid(bid).queryFirst();
		Integer onProcess = yspProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
		model.addAttribute("onProcess", onProcess);
		
		return "finance/tcwRunningBack/toFlow";
	}
}